package com.gem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gem.entity.Order;
import com.gem.entity.Product;
import com.gem.entity.ProductCategory;
import com.gem.entity.User;
import com.gem.service.OrderService;
import com.gem.service.ProductCategoryService;
import com.gem.service.ProductService;
import com.gem.service.UserService;
import com.gem.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static com.gem.util.PageUtil.PAGESIZE;

/**
 * @author 刘聪
 * Created  on 2021/4/1
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService categoryService;

    @Value("${images}")
    private String targetPath;

    @GetMapping("/index")
    public String index(HttpSession session) {
        Object user = session.getAttribute("User");

        return "admin/index";
    }

    @GetMapping("/userList")
    public String userList(@RequestParam(defaultValue = "1", required = false) Integer pageNum, Model model) {
        Page allUser = userService.selectAll(pageNum);
        List<User> userList = allUser.getRecords();
        model.addAttribute("userList", userList);

        model.addAttribute("PageNum", pageNum);
        model.addAttribute("PageTotal", allUser.getPages());
        return "admin/userList";
    }

    @GetMapping("/orderList")
    public String orderList(@RequestParam(defaultValue = "1", required = false) Integer pageNum, Model model) {
        Page<Order> page = new Page<>(pageNum, PAGESIZE);
        QueryWrapper wrapper = new QueryWrapper();
        Page orderPage = orderService.page(page, wrapper);
        List<Order> orderList = orderPage.getRecords();
        List<OrderVO> voList = orderService.selectAll(orderList);
        model.addAttribute("orderList", voList);

        model.addAttribute("PageNum", pageNum);
        model.addAttribute("PageTotal", orderPage.getPages());
        return "admin/orderList";
    }

    @GetMapping("/productList")
    public String productList(@RequestParam(defaultValue = "1", required = false) Integer pageNum, Model model) {
        Page allProduct = productService.selectAll(pageNum);
        List<Product> productList = allProduct.getRecords();
        model.addAttribute("productList", productList);

        model.addAttribute("PageNum", pageNum);
        model.addAttribute("PageTotal", allProduct.getPages());
        return "admin/productList";
    }

    @GetMapping("/addProduct")
    public String addProduct(Model model) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("sort_order", 2);
        List<ProductCategory> cateList = categoryService.list(wrapper);
        model.addAttribute("cateList",cateList);
        return "admin/addProduct";
    }


    @RequestMapping("/upload")
    public String upload(String title,Integer selectGroup,Double price,Integer stock,MultipartFile imgfile,String detail, Model model) throws IOException {

        String fileName = "";
//        获取后缀名
        fileName = imgfile.getOriginalFilename();//包含上传文件的后缀名
        String suffix = fileName.substring(fileName.lastIndexOf("."));
//        文件重命名
        fileName = UUID.randomUUID().toString().replace("-", "").toLowerCase() + suffix;
        Product product = new Product();
        product.setName(title);
        product.setCategoryId(selectGroup);
        product.setPrice(price);
        product.setDetail(detail);
        product.setStock(stock);
        product.setSubImages(fileName);
        productService.save(product);
        imgfile.transferTo(new File(targetPath, fileName));
        model.addAttribute("Img", fileName);
        return "redirect:/addProduct";
    }
}
